<template>
    <div class="cmt-container">
        <h4>发表评论</h4>
        <hr>
        <textarea v-model="msg" placeholder="请输入吐槽内容（不超过120字）" maxlength="120"></textarea>
        <mt-button type="primary" size="large" v-on:click="postComment()">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, index) in comments" :key="index">
                <div class="cmt-title">
                    第{{index+1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}
                </div>
                <div class="cmt-body">
                    {{item.comment}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain v-on:click="getMore()">加载评论</mt-button>
    </div>
</template>

<script>
import {Toast} from 'mint-ui'
export default {
    data() {
        return {
            pageIndex: 1,
            comments: [],
            msg: ''
        }
    },
    methods: {
        getComments(){
            this.$http.get('output/newscomments/'+ this.id +'/comment.php?pageindex='+ this.pageIndex).then(function(result){
                if (result.body.status===0) {
                    //console.log(result.body.message);
                    this.comments=this.comments.concat(result.body.message)
                }else{
                    Toast("评论加载失败。。。")
                }
            })
        },
        getMore(){
            this.pageIndex++
            this.getComments()
        },
        postComment(){
            if (this.msg.trim().length===0) {
                return Toast('评论内容不能为空！')
            }
            this.$http.post('output/postcomments/index.php?id='+this.id,{
                content: this.msg.trim()
            }).then(function(result){
                if (result.body.status===0) {
                    var cmt = {
                        user_name: '匿名用户',
                        add_time: Date.now(),
                        comment: this.msg.trim()
                    }
                    this.comments.unshift(cmt)
                    this.msg=''
                }else{
                    Toast("发表评论失败。")
                }
            })
        }
    },
    created() {
        this.getComments()
    },
    props: [
        "id"
    ]
}
</script>

<style lang="scss" scoped>
.cmt-container{
    textarea{
        font-size: 14px;
        margin: 0;
    }
    .cmt-list{
        margin-top: 5px;
        .cmt-item{
            font-size: 12px;
            .cmt-title{
                line-height: 30px;
                background-color: #ccc;
            }
            .cmt-body{
                line-height: 30px;
                text-indent: 2em;
            }
        }
    }
}
</style>

